package baseFx.common.utils;

import java.util.Base64;

public class ByteUtils {
    public static byte[] hex2Byte(String hexString) {
        byte[] ret = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length(); i += 2) {
            ret[i / 2] = Integer.decode("0x" + hexString.substring(i, i + 2)).byteValue();
        }
        return ret;
    }

    public static String byte2Hex(byte[] data) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (byte b : data) {
            stmp = (Integer.toHexString(b & 0XFF));
            if (stmp.length() == 1) {
                hs.append("0");
            }
            hs.append(stmp);
        }
        return hs.toString().toUpperCase();
    }

    public static String encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static byte[] decode(String input) {
        return Base64.getDecoder().decode(input);
    }
}
